<template>
  <div class="read-page">
    <!-- header -->
    <van-nav-bar
      :title="$route.params.name"
      right-text="目录"
      left-arrow
      @click-left="$router.go(-1)"
      @click-right="isShowList = !isShowList"
    />
    <!-- body -->
    <div v-if="!loading" class="body" v-show="isShowList === false">
      <div class="read-contain" v-html="readData.htmlOfBody"></div>
    </div>
    <div v-if="loading" class="loading">
      <van-loading size="24px" vertical>加载中...</van-loading>
    </div>
    <!-- 目录 -->
    <div class="read-list" v-show="isShowList === true">
      <p
        class="read-list-item"
        v-for="c in readData.chapters"
        :key="c.id"
        :class="c.isFree ? 'free' : ''"
        @click="toread(c.isFree, c.id, c.subject)"
      >
        {{ c.subject }}
        <span class="read" v-if="c.isFree">阅读</span>
      </p>
    </div>
    <!-- 底部 -->
    <div class="bottom">
      <span class="chapt" :class="id == chaptersArr[0]?'end':''" @click="toLast()">上一章</span>
      <span class="chapt" :class="id == chaptersArr[readLength]?'end':''" @click="toNext()">下一章</span>
    </div>
  </div>
</template>

<script>
import { ReadAPI } from "@/apis";
export default {
  props: ["id"],
  data() {
    return {
      loading: true,
      // 数据
      readData: {},
      // 是否展示目录
      isShowList: false,
      // 数组： 用于记录id值
      chaptersArr: [],
      // 数组： 用于记录isFree值
      isFreeArr: [],
    };
  },
  mounted() {
    this.loading = true
    this.getRead();
  },
  watch: {
    // 监视id的更新
    id() {
      this.getRead();
    },
  },
  computed: {
    // 计算可读章节
    readLength(){
      let trueArr = this.isFreeArr.filter(item => item == true);
      return trueArr.length - 1 ;
    }
  },
  methods: {
    // 网络请求
    async getRead() {
      let { data } = await this.$http(ReadAPI(this.id));
      this.readData = data;
      // 数组： 用于记录id值
      this.chaptersArr = data.chapters.map((item) => item.id);
      // 数组： 用于记录isFree值
      this.isFreeArr = data.chapters.map((item) => item.isFree);
      this.$nextTick(() => {
        this.loading = false
      })
    },
    // to read
    toread(status, id, name) {
      if (status && id != this.id) {
        this.$router.replace({
          name: `read`,
          params: {
            id: id,
            name: name,
          },
        });
        this.isShowList = false;
      }
    },
    // toLast
    toLast() {
      this.chaptersArr.forEach((item, index) => {
        if (item == this.id) {
          if (this.chaptersArr[index - 1] && this.isFreeArr[index - 1]) {
            this.$router.replace(`/read/${this.chaptersArr[index - 1]}`);
            this.isShowList = false
          }
        }
      });
    },
    // toNext
    toNext() {
      this.chaptersArr.forEach((item, index) => {
        if (item == this.id) {
          if (this.chaptersArr[index + 1] && this.isFreeArr[index + 1]) {
            this.$router.replace(`/read/${this.chaptersArr[index + 1]}`);
            this.isShowList = false
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.read-page {
  position: relative;
  width: 100vw;
  background-color: #fff;
  z-index: 999;
  .van-nav-bar {
    width: 100vw;
    position: fixed;
    top: 0;
  }
  .body {
    background-color: #fff;
    padding: 70px 20px 55px 20px;
  }
  .read-list {
    padding: 0 15px;
    p.read-list-item {
      position: relative;
      color: #000;
      font-size: 14px;
      line-height: 20px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      padding: 15px 15px 15px 0;
      border-top: 1px solid var(--more-bg-white);
      &.free {
        color: var(--theme-blue);
      }
      .read {
        background-color: var(--more-bg-white);
        color: var(--more-font-grey);
        padding: 0 8px;
        position: absolute;
        right: 0;
      }
    }
  }
}
.bottom {
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  width: 100vw;
  padding: 0 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .chapt {
    line-height: 26px;
    color: var(--theme-blue);
    font-size: 14px;
    &.end{
      color: #c8cdd4;
    }
  }
}
</style>